/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kylingroup.JavaFX.TreeView;

import java.io.File;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TreeItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;

/**
 *
 * @author kylin
 */
public class UploadListItem{
    private String id=null;
    private GridPane box=null;
    private ImageView icon=null;
    private Label lbfilename=null;
    private Label lbstatus=null;
    private ImageView btnstart=null;
    private ImageView btncancle=null;
    private ProgressIndicator progress=null;
    private LocalFileTransferTask transfertask=null;
    private String destPath;

    public UploadListItem(File file) {
        box=new GridPane();
        box.setAlignment(Pos.CENTER_LEFT);
        box.setPadding(new Insets(5,5,5,5));
        box.setVgap(5);
        box.setHgap(5);
        HBox.setHgrow(box,Priority.ALWAYS);
                
        
        lbfilename=new Label(file.getName());
        lbfilename.setPrefWidth(500);
        lbfilename.setPadding(new Insets(0,50,0,10));
        setIcon(file);
        
        lbstatus=new Label("ready");
        lbstatus.setPadding(new Insets(0,10,0,10));

        btnstart=new ImageView(new Image(getClass().getResourceAsStream("start.png")));
        btnstart.setFitWidth(25);
        btnstart.setFitHeight(25);
        
        btncancle=new ImageView(new Image(getClass().getResourceAsStream("delete2.png")));
        btncancle.setFitWidth(25);
        btncancle.setFitHeight(25);
        
        progress=new ProgressIndicator();
        progress.setPrefSize(30, 30);
        progress.setPadding(new Insets(0,10,0,10));
        progress.setProgress(0);

        box.addColumn(0,lbfilename);
        box.addColumn(1,lbstatus);
        box.addColumn(2,progress);
        box.addColumn(3,btnstart);
        box.addColumn(4,btncancle);
    }
    
    public void setIcon(File file){
        String type=file.getName();
        type=type.substring(type.lastIndexOf(".")+1,type.length());
        Image image=FileBrowserController.typeicons.get(type);
        if(image==null){
            image=FileBrowserController.typeicons.get("default");
        }
        icon=new ImageView();
        icon.setFitWidth(35);
        icon.setFitHeight(44);
        icon.setImage(image);
        lbfilename.setGraphic(icon);
    }
    
    public GridPane getBox() {
        return box;
    }
    
    public Label getLbfilename() {
        return lbfilename;
    }

    public Label getLbstatus() {
        return lbstatus;
    }

    public ImageView getBtnunstart() {
        return btnstart;
    }
    
    public ImageView getBtncancle() {
        return btncancle;
    }

    public LocalFileTransferTask getTransfertask() {
        return transfertask;
    }

    public void setTransfertask(LocalFileTransferTask transfertask) {
        this.transfertask=transfertask;

        progress.progressProperty().bind(this.transfertask.getProgressProperty());
        
        EventType<WorkerStateEvent> type=WorkerStateEvent.WORKER_STATE_SUCCEEDED;
        EventHandler<WorkerStateEvent> handler=new EventHandler<WorkerStateEvent>(){
            @Override
            public void handle(WorkerStateEvent event){
                System.out.println("success");
                transfertask.getSuccessInfo();
                if(FileBrowserController.delemode) {
                    transfertask.getFile().delete();
                }
            }
        };
        this.transfertask.setOnFinishHander(type, handler);
    }
    
    public String getDestPath() {
        return destPath;
    }

    public void setDestPath(String destPath) {
        this.destPath = destPath;
    }
}
